package gsw.demo.mvp.mvp_rxbus;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.List;

import javax.inject.Inject;

import gsw.demo.mvp.R;
import gsw.demo.mvp.mvp_rxbus.bean.Girl;
import gsw.demo.mvp.mvp_rxbus.dagger.GirlModule;
import gsw.demo.mvp.mvp_rxbus.presenter.GirlPresenter;
import gsw.demo.mvp.mvp_rxbus.view.IGirlView;
import gsw.demo.mvp.mvp_rxbus.view.adapter.GirlAdapter;
import gsw.demo.mvp.core.net.rxservice.rxbus.RxBus;
import gsw.demo.mvp.mvp_rxbus.dagger.DaggerGirlComponent;

public class ActivityMvpRxBus extends Activity implements IGirlView {
    @Inject
    GirlPresenter presenter;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp_rxbus);
        list = findViewById(R.id.listview);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().unRegister(presenter);
    }

    @Override
    public void showGirls(List<Girl> girls) {
        //model层的数据在girls中返回了
        list.setAdapter(new GirlAdapter(this, girls));
    }

    /**
     * 展示RxBus架构的MVP
     */
    public void show1(View view) {
        //presenter = new GirlPresenter(this);

        //使用Dagger的build()，来注册对象
        DaggerGirlComponent.builder().girlModule(new GirlModule(this)).build().inject(this);

        //注册表示层，获取到数据后，自动发送给表示层
        RxBus.getInstance().register(presenter);
    }
}
